let obj = {}
let pageIsEnd = false
let pageStart = 0
let pageCount = 12
let postIsEnd = false


window.onload = function () {
    init()

//     监听
    Object.defineProperty(obj, 'listener', {
        set: function (value) {
            if (value === true) {
                if (pageIsEnd === true) {
                    console.log("已经在刷新页面了")
                } else {
                    if (postIsEnd === true) {
                        console.log("全部帖子加载完毕拉")
                    } else {
                        //页面加载通知
                        pageIsEnd = true
                        console.log('加载页面')
                        loadPost(pageStart, pageCount)
                    }
                }
            } else {
                pageIsEnd = false
            }
        }
    })

}


window.onscroll = function () {
    //文档高
    let scrollHeight = document.documentElement.scrollHeight
    //滚动条距离
    let scrollTop = document.documentElement.scrollTop
    //窗口高
    let clientHeight = document.documentElement.clientHeight

    if (clientHeight + scrollTop >= scrollHeight - 10) {
        obj.listener = true
    }
}

async function init() {
    await initRootTop()
    await initRootFooter()

    await loadPost()
    await initLogin()
    // 检查是否登录
    await toLogin(true)


    // 侧边栏
    await initMainBar()
    await initHotPost()
    await initAdvertisement()

    //加载遮罩
    document.body.style = "display:block"
    // document.body.style.animation = 'bodyShow 0.2s'


}

function send_post() {
    window.location.href = window.webUrl + "/sendPost.html"
}


// 向服务器请求post帖子信息
function loadPost(start, count, searchContext) {
    ajax('get', '/post/getpost?start=' + (start === undefined ? pageStart : start) + '&count=' + (count === undefined ? pageCount : count) + (searchContext === undefined ? '' : '&searchContext=' + searchContext.value), null, function (json) {
            // console.log(json);
            let r = json.data.map(({
                                       commentCount,
                                       postCommunity,
                                       postCreateTime,
                                       postId,
                                       postReadCount,
                                       postState,
                                       postTitle,
                                       postUpdateTime,
                                       postUserId,
                                       postOperator,
                                       whatTags
                                   }) => {

                let tag = whatTags.map(({
                                            tagTitle
                                        }) => {
                    return `<li>${tagTitle}</li>`
                }).toString().split(",")
                tagString = ''
                for (var i = 0; i < tag.length; i++) {
                    tagString += tag[i]
                }

                return `<li class="content-list-item">
								<div class="item-state">
									<div class="item-recover ${commentCount === 0 ? 'item-state-without' : (commentCount >= 3 ? 'item-state-hot' : 'item-state-common')}">
										<p class="recover-count">${commentCount}</p>
										<span>回答</span>
									</div>
									<div class="item-read ${postReadCount === 0 ? 'item-state-without' : (postReadCount >= 10 ? 'item-state-hot' : 'item-state-common')}">
										<p class="read-count">${postReadCount}</p>
										<span>阅读</span>
									</div>
								</div>
								<div class="item-info">
									<div class="item-info-title">
										<a href="${window.webUrl}/postInfo.html?postId=${postId}">${postTitle}</a>
									</div>
									<div class="item-label">
										<div class="tag">
											<ul>
											${tagString}
											</ul>
										</div>
										<div class="by-user">由<span>${postOperator}</span> <span>${formatDate(postUpdateTime > postCreateTime ? postUpdateTime : postCreateTime)}</span> <span>${postUpdateTime > postCreateTime ? '回复' : '创建'}</span>
										</div>
									</div>
								</div>
							
						</li>`
            })
            // console.log(r)
            data = r.toString().split(',')
            htmlData = ''
            for (var i = 0; i < data.length; i++) {
                htmlData += data[i]
            }

            let post_list = document.querySelector("#post-list")
            post_list.innerHTML += htmlData

            if (json.data.length !== 0) {
                pageStart = pageStart + json.data.length
                obj.listener = false
            } else {
                postIsEnd = true
                obj.listener = false
            }
        },
        function (json) {

        })
}